package com.demo.mqtt.zigbee.entity.po.WaveUpload;

import com.demo.mqtt.common.utils.IotStringUtil;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDateTime;

/**
 * @program: mqtt
 * @description: 波形上传原始数据
 * @author: liuYunKai
 * @create: 2021-03-26 13:13
 */
@Data
@Document(collection = "zigbee_wave_upload")
public class WavesUpLoadPO {
    @Id
    private String id;
    //SN序列号
    private String sn;
    //波形数据时间
    private String waveDate;
    private String waveTime;
    //波形方向
    private short waveDir;
    //波形类型
    private short waveType;
    //波形状态
    private short waveState;
    //波形长度（采集到的点数）
    private short waveLen;
    //转换系数
    private Float waveCoe;
    //波形分析频率
    private Integer waveFreq;
    //本报波形数据长度

    //本报采集点数据
    @Field("data")
    private Float[] data;

    public WavesUpLoadPO(WaveUpload waveUpload) {
        this.sn = waveUpload.SN.get();

        LocalDateTime waveTime = IotStringUtil.getTimeFromUnsigned8(waveUpload.waveTime);
        this.waveDate = waveTime.toLocalDate().toString();
        this.waveTime = waveTime.toLocalTime().toString();

        this.waveDir = waveUpload.waveDir.get();
        this.waveType = waveUpload.waveType.get();
        this.waveState = waveUpload.waveState.get();
        this.waveLen = waveUpload.waveLen.get();
        this.waveCoe = waveUpload.waveCoe.get();
        this.waveFreq = waveUpload.waveFreq.get();

        int length = waveUpload.DATA.length;
        this.data = new Float[length];
        for (int i = 0; i < waveUpload.DATA.length; i++) {

            try {
                data[i] = waveUpload.DATA[i].get();
            }catch (Exception e){
                break;
            }


        }
    }
}
